package stsj.meddroid.screen;

import stsj.meddroid.MainAppListener;
import stsj.meddroid.Resources;
import stsj.meddroid.Settings;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actors.Button;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.badlogic.gdx.scenes.scene2d.actors.Button.ClickListener;

public class StageSelectScreen implements Screen, InputProcessor {

	private MainAppListener mainListener;

	private Stage stage;
	private Image bg;
	
	private Camera camera;
	
	public  TextureRegion bgStageSelectRegion ; 


	
	private TextureRegion stage01Region[];
	private TextureRegion stage02Region[];
	private TextureRegion stage03Region[];
	private TextureRegion stage04Region[];
	private TextureRegion stage05Region[];
	
	
	
	private Button stage01Button [];
	private Button stage02Button [];
	private Button stage03Button [];
	private Button stage04Button [];
	private Button stage05Button [];
	


	
	private Button backButton;

	public StageSelectScreen(MainAppListener mainListener) {
		// TODO Auto-generated constructor stub
		this.mainListener = mainListener;
		camera = Resources.camera;
		camera.position.set(240, 160, 0);
		
		bgStageSelectRegion = Resources.bgStageSelectRegion;
		
		
		stage01Region = new TextureRegion[3];
		stage02Region = new TextureRegion[3];
		stage03Region = new TextureRegion[3];
		stage04Region = new TextureRegion[3];
		stage05Region = new TextureRegion[3];
		
		stage01Region[0] = new TextureRegion(Resources.stageTex,0,1,90,90);
		stage01Region[1] = new TextureRegion(Resources.stageTex,90,1,90,90);
		stage01Region[2] = new TextureRegion(Resources.stageTex,180,1,90,90);
		
		stage02Region[0] = new TextureRegion(Resources.stageTex,0,90,90,90);
		stage02Region[1] = new TextureRegion(Resources.stageTex,90,90,90,90);
		stage02Region[2] = new TextureRegion(Resources.stageTex,180,90,90,90);
		
		
		stage03Region[0] = new TextureRegion(Resources.stageTex,270,2,90,90);
		stage03Region[1] = new TextureRegion(Resources.stageTex,360,2,90,90);
		stage03Region[2] = new TextureRegion(Resources.stageTex,450,2,90,90);
		
		stage04Region[0] = new TextureRegion(Resources.stageTex,270,90,90,90);
		stage04Region[1] = new TextureRegion(Resources.stageTex,360,90,90,90);
		stage04Region[2] = new TextureRegion(Resources.stageTex,450,90,90,90);
		
		stage05Region[0] = new TextureRegion(Resources.stageTex,540,1,90,90);
		stage05Region[1] = new TextureRegion(Resources.stageTex,630,1,90,90);
		stage05Region[2] = new TextureRegion(Resources.stageTex,720,1,90,90);
		
		
		
		
		
		
		
		
		
		
		
		
		
		stage = new Stage(480, 320, true);
		

		bg = new Image("Stage Background", bgStageSelectRegion);		

		stage01Button = new Button[2];
		stage02Button = new Button[2];
		stage03Button = new Button[2];
		stage04Button = new Button[2];
		stage05Button = new Button[2];
		
		
		stage01Button[0] = new Button("Stage 01 Button", stage01Region[0],
				stage01Region[1]);
		stage01Button[0].x = stage.width() * 0.20f;
		stage01Button[0].y = stage.height() * 0.60f;
		stage01Button[0].clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
				setState(MainAppListener.STAGE_1_ROUND);
			}

		};

		stage02Button[0] = new Button("Stage 02 Button", stage02Region[0],
				stage02Region[1]);
		stage02Button[0].x = stage.width() * 0.40f + 15;
		stage02Button[0].y = stage.height() * 0.60f;
		stage02Button[0].clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
				setState(MainAppListener.STAGE_2_ROUND);
			}

		};

		stage03Button[0] = new Button("Stage 03 Button", stage03Region[0],
				stage03Region[1]);
		stage03Button[0].x = stage.width() * 0.60f + 30;
		stage03Button[0].y = stage.height() * 0.60f;
		stage03Button[0].clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
				setState(MainAppListener.STAGE_3_ROUND);
			}

		};

		stage04Button[0] = new Button("Stage 04 Button", stage04Region[0],
				stage04Region[1]);
		stage04Button[0].x = stage.width() * 0.20f;
		stage04Button[0].y = stage.height() * 0.25f;
		stage04Button[0].clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
				setState(MainAppListener.STAGE_4_ROUND);
			}

		};

		stage05Button[0] = new Button("Stage 05 Button", stage05Region[0],
				stage05Region[1]);
		stage05Button[0].x = stage.width() * 0.40f + 15;
		stage05Button[0].y = stage.height() * 0.25f;
		stage05Button[0].clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
				setState(MainAppListener.STAGE_5_ROUND);
			}

		};
		
		backButton = new Button("Back Button", Resources.backButton[0],Resources.backButton[1]);
		backButton.x = 10;
		backButton.y = 10;
		backButton.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.backSound);
				setState(MainAppListener.MAIN_MENU);
			}

		};
		

		stage02Button[1] = new Button("Stage 02 Button U", stage02Region[2]);
		stage02Button[1].x = stage02Button[0].x;
		stage02Button[1].y = stage02Button[0].y;

		
		stage03Button[1] = new Button("Stage 03 Button U", stage03Region[2]);
		stage03Button[1].x = stage03Button[0].x;
		stage03Button[1].y = stage03Button[0].y;

		stage04Button[1] = new Button("Stage 04 Button U", stage04Region[2]);
		stage04Button[1].x = stage04Button[0].x;
		stage04Button[1].y = stage04Button[0].y;

		stage05Button[1] = new Button("Stage 05 Button U", stage05Region[2]);
		stage05Button[1].x = stage05Button[0].x;
		stage05Button[1].y = stage05Button[0].y;
		
		stage02Button[1].clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.lockedSound);
			}

		};
		stage03Button[1].clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.lockedSound);
			}

		};
		stage04Button[1].clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.lockedSound);
			}

		};
		stage05Button[1].clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.lockedSound);
			}

		};
		
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		Gdx.input.setInputProcessor(mainListener.mainMenuScreen);
		dispose();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render(float arg0) {
		// TODO Auto-generated method stub
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears
													// the screen.
		camera.update();
		
		
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30.0f));
		stage.draw();

	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
		Gdx.input.setCatchBackKey(false);
		stage.clear();
		
		
		Boolean stage01Unlocked = Settings.stagesUnlocked[1];
		Boolean stage02Unlocked = Settings.stagesUnlocked[2];
		Boolean stage03Unlocked = Settings.stagesUnlocked[3];
		Boolean stage04Unlocked = Settings.stagesUnlocked[4];
		Boolean stage05Unlocked = Settings.stagesUnlocked[5];
		
		stage.addActor(bg);
		stage.addActor(stage01Button[0]);
		stage.addActor(backButton);
		
//		stage01.action(FadeIn.$(1f));
//		backButton.action(FadeIn.$(1f));

		if (stage02Unlocked) {
			stage.addActor(stage02Button[0]);
//			stage02.action(FadeIn.$(1f));
		} else {
			stage.addActor(stage02Button[1]);
//			stage02U.action(FadeIn.$(1f));
		}

		if (stage03Unlocked) {
			stage.addActor(stage03Button[0]);
//			stage03.action(FadeIn.$(1f));
		} else {
			stage.addActor(stage03Button[1]);
//			stage03U.action(FadeIn.$(1f));
		}

		if (stage04Unlocked) {
			stage.addActor(stage04Button[0]);
//			stage04.action(FadeIn.$(1f));
		} else {
			stage.addActor(stage04Button[1]);
//			stage04U.action(FadeIn.$(1f));
		}

		if (stage05Unlocked) {
			stage.addActor(stage05Button[0]);
//			stage05.action(FadeIn.$(1f));
		} else {
			stage.addActor(stage05Button[1]);
//			stage05U.action(FadeIn.$(1f));
		}
		Gdx.input.setInputProcessor(this);
		
//		bg.action(FadeIn.$(1f));
////		bg.action(FadeIn.$(1f).setCompletionListener(new OnActionCompleted(){
//
//			@Override
//			public void completed(Action arg0) {
//				// TODO Auto-generated method stub
//				setInputProcessor();
//			}
//			
//		}));
		
		
		

//		Gdx.input.setInputProcessor(this);
	}

	@Override
	public boolean keyDown(int arg0) {
		// TODO Auto-generated method stub
	
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		if (arg0 == Input.Keys.BACK) {
			Resources.playSound(Resources.backSound);
			setState(MainAppListener.MAIN_MENU);
			return false;
		}
		return false;
		
	}

	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {

		return stage.touchDown(arg0, arg1, arg2, arg3);
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {

		return stage.touchDragged(arg0, arg1, arg2);
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {

		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {

		return stage.touchUp(arg0, arg1, arg2, arg3);
	}

	private void setState(int state) {
		
		mainListener.setState(state);
		dispose();
	}
	
	public void setInputProcessor(){
		
	}

}
